Skip to content

File

The File class from the java.io package, allows us to work with files.

To use the File class, create an object of the class, and specify the filename or directory name

API

  • Contructors
File(String parent,String child)
File(File parent, String child)
File(String name)
  • File Path
File.separator: Platform dependent default name-separator character as String. For windows, it’s ‘\’ and for unix it’s ‘/’.
File.separatorChar: Same as separator but it’s char.
File.pathSeparator: Platform dependent variable for path-separator. For example PATH or CLASSPATH variable list of paths separated by ‘:’ in Unix systems and ‘;’ in Windows system.
File.pathSeparatorChar: Same as pathSeparator but it’s char.

getName(): Returns the name of the file or directory denoted by this abstract pathname.
getPath(): Converts this abstract pathname into a pathname string.
getAbsolutePath(): Returns the absolute pathname string of this abstract pathname.
getParent(): Returns the pathname string of this abstract pathname's parent, or null if this pathname does not name a parent directory.
  • File Status
exists(): Tests whether the file or directory denoted by this abstract pathname exists.
isFile(): Tests whether the file denoted by this abstract pathname is a normal file.
isDirectory(): Tests whether the file denoted by this abstract pathname is a directory.
length(): Returns the length of the file denoted by this abstract pathname. T
  • Create File
createNewFile(): Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist
delete(): Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.
mkdir(): Creates the directory named by this abstract pathname.
mkdirs(): Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories.
  • List File
list(): Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
listFiles(): Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.
listRoots(): List the available filesystem roots.

File Path

import java.io.File;

public class FileDemo {
    public static void main(String[] args) {
        String path = "D:\\java\\IO\\IO.png";
        System.out.println(File.separator);

        path = "D:/java/IO/IO.png";

        path = "D:" + File.separator +"java" + File.separator + "IO " + File.separator + "IO.png";
        System.out.println(path);

        path = "src/images/ball.png";

        File src = new File(path);
        System.out.println(src.length());
        System.out.println(src.getAbsolutePath());
        System.out.println(System.getProperty("user.dir"));

        src = new File("src/images/", "ball.png");
        System.out.println(src.length());

        src = new File(new File("src/images/"), "ball.png");
        System.out.println(src.length());

        System.out.println("name: " + src.getName());
        System.out.println("path: " + src.getPath());
        System.out.println("absolute path: " + src.getAbsolutePath());
        System.out.println("parent path: " + src.getParent());
    }
}

File Status

import java.io.File;

public class FileDemo {
    public static void main(String[] args) {
        String path = "src/images/ball.png";

        File src = new File(path);
        System.out.println("exist: " + src.exists());
        System.out.println("is file: " + src.isFile());
        System.out.println("is directory: " + src.isDirectory());
        System.out.println(src.length());
    }
}

Create File

import java.io.File;

public class FileDemo {
    public static void main(String[] args) throws IOException {
        File src = new File("src/images/io.txt");
        boolean flag = src.createNewFile();
        System.out.println("create new file: " + flag);
        flag = src.delete();
        System.out.println("delete new file: " + flag);

        File dir = new File("src/images/dirs/dir");

        flag = dir.mkdir();
        System.out.println("flag: " + flag);
        flag = dir.mkdirs();
        System.out.println("flag: " + flag);
    }
}

List File

import java.io.File;

public class FileDemo {
    static int len = 0;
    public static void main(String[] args) throws IOException {
        File dir = new File("src");

        String[] subList = dir.list();

        for(String s: subList) {
            System.out.println("s: " + s);
        }

        File[] subFile = dir.listFiles();
        for(File f : subFile) {
            System.out.println(f.getAbsolutePath());
        }

        File[] roots = File.listRoots();

        for(File f : roots) {
            System.out.println(f.getAbsolutePath());
        }
        System.out.println(count(src));
    }
    public static int count(File src) {
        if(null == src || !src.exists()) return 0;
        for(File f : src.listFiles()) {
            if(f.isFile()) {
                len += f.length();
            }else {
                len += count(f);
            }
        }
        return len;
    }
}

Authors

  • Weiduo Sun

License

This project is licensed under the MIT License - see the LICENSE.md file for details